package benny_driving_ser.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import benny_driving_ser.ObjectBean.UserInfoBean;
import benny_driving_ser.dao.UserInfoDao;
import benny_driving_ser.util.DBUtil;

public class UserInfoDaoImpl implements UserInfoDao {

	
	/**
	 * 加载用户信息
	 */
	public List<UserInfoBean> loadUserInfo(int id) {
		
		List<UserInfoBean> list;
		UserInfoBean userInfo=null;
		
		String sql = "select pj.dwmc,pj.sjdz,hy.xm,hy.sjh,hy.add1,hy.add2,hy.add3," +
				"hy.add4,hy.add5,hy.platenum1,hy.platenum2,hy.platenum3,hy.platenum4" +
				",hy.qsxm,hy.qsdh from t_hy hy,t_pj pj where hy.ID= "+id+"  and hy.ID=pj.userId";
		
		Connection con = DBUtil.getInstance().openConnection();
		list = new ArrayList<UserInfoBean>();
		try {
			con.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
			Statement s = con.createStatement();
			ResultSet rs = s.executeQuery(sql);
			while (rs.next()) {
				userInfo = new UserInfoBean();
				userInfo.setName(rs.getString("xm"));
				userInfo.setAdd1(rs.getString("add1"));
				userInfo.setAdd2(rs.getString("add2"));
				userInfo.setAdd3(rs.getString("add3"));
				userInfo.setAdd4(rs.getString("add4"));
				userInfo.setAdd5(rs.getString("add5"));
				userInfo.setBilladd(rs.getString("sjdz"));
				userInfo.setBillhead(rs.getString("dwmc"));
				userInfo.setPlatenum1(rs.getString("platenum1"));
				userInfo.setPlatenum2(rs.getString("platenum2"));
				userInfo.setPlatenum3(rs.getString("platenum3"));
				userInfo.setPlatenum4(rs.getString("platenum4"));
				userInfo.setRname(rs.getString("qsxm"));
				userInfo.setRtel(rs.getString("qsdh"));
				userInfo.setTel(rs.getString("sjh"));
				
				list.add(userInfo);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		DBUtil.getInstance().closeConnection(con);
		
		//System.out.println("几条数据"+list.size());
		return list;
	}
	
	
	/**
	 * 修改司机密码
	 */
	public int updateDriPwd(int driId, String pwd) {
		//pwd = "long";
		String sql = "update t_user SET userpwd = "+pwd+" where id = "+driId;
		Connection con = DBUtil.getInstance().openConnection();
		try {
			con.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
			PreparedStatement ps = con.prepareStatement(sql);
			ps.executeUpdate();
			DBUtil.getInstance().closeConnection(con);
			return 0;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 1;
	}

}
